package com.ywk.base.dataStructure.queue;

import java.util.Scanner;


//用数组实现队列缺点：数组不能复用(即：只能用一次)
public class ArrayQueueDemo {

	public static void main(String[] args) {
		ArrayQueue query=new ArrayQueue(3);
		int data;
		char key=' ';
		boolean loop=true;
		Scanner input=new Scanner(System.in);
		while(loop) {
			System.out.println("s(show):显示队列");
			System.out.println("e(exit):退出程序");
			System.out.println("a(add):添加队列数据");
			System.out.println("g(get):取出队列数据");
			System.out.println("h(head):查看队列头数据");
			System.out.print("请选择：");
			key=input.next().charAt(0);
			switch (key) {
			case 's':
				try {
					query.showQueue();
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
				break;
			case 'e':
				input.close();
				loop=false;
				break;
			case 'a':
				System.out.print("添加数据：");
				data=input.nextInt();
				query.addQueue(data);
				break;
			case 'g':
				try {
					int value=query.getQueue();
					System.out.printf("取出的数据:%d\n",value);
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
				break;
			case 'h':
				try {
					query.headQueue();
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
				break;
			default:
				break;
			}
		}
		System.out.println("程序已退出");
	}
}

class ArrayQueue {
	private int maxSize;
	//对尾,指的就是队尾的数(包含),(初值为-1)
	private int rear;
	//对头,arrayQueue[font]指的是取出去的数，此时的队列是不包含该数值（不包含），
	//arrayQueue[font+1]才是真正意义上的对头,(初值为-1)
	private int front;
	private int[] arrayQueue;
	public ArrayQueue(int arrayMaxSize) {
		maxSize=arrayMaxSize;
		rear=-1;
		front=-1;
		arrayQueue=new int[maxSize];
	}
	
	public boolean isFull() {
		return rear-front==maxSize;
	}
	
	public boolean isEmpty() {
		return rear==front;
	}
	
	public void addQueue(int data) {
		if(isFull()) {
			System.out.println("队列满了...不能添加..");
			return;
		}
		rear++;
		arrayQueue[rear]=data;
	}
	
	public int getQueue() {
		if(isEmpty()) {
			throw new RuntimeException("队列为空...");
		}
		front++;
		return arrayQueue[front]; 
	}

	public void showQueue() {
		if(isEmpty()) {
			throw new RuntimeException("队列为空...");
		}
		if(rear<maxSize) {
			for (int i = front+1; i < arrayQueue.length; i++) {
			System.out.printf("编号为%d的个数：%d\n",i,arrayQueue[i]);
			}
		}
	}
	
	public void headQueue() {
		if(isEmpty()) {
			throw new RuntimeException("队列为空...");
		}
		System.out.printf("队列的头信息：%d\n",arrayQueue[front+1]);
	}
}